package com.aerfazhe.order.service.impl;

import com.aerfazhe.order.feign.StockClient;
import com.aerfazhe.order.mapper.OrderMapper2;
import com.aerfazhe.order.pojo.Order;
import com.aerfazhe.order.service.OrderService2;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author：wk
 * @Slogan：无论风雨，和自己一决胜负吧
 * @Create：2022/7/21/16:17
 * @Description：下单服务业务接口实现
 * @Version：1.0
 */
@Service
@Slf4j
public class OrderService2Impl implements OrderService2 {

    @Resource
    private OrderMapper2 orderMapper2;

    @Autowired
    private StockClient stockClient;

    @Override
//    @Transactional
    @GlobalTransactional
    @Trace
    @Tags({
            @Tag(key = "saveOrder",value = "arg[0]")
            ,@Tag(key = "saveOrder",value = "returnedObj")
    })
    public Map<String, Object> saveOrder(Order order) {
        int i = orderMapper2.saveOrder(order);
        log.info("下单状态：{}",i);
        Map<String,Object> map = new HashMap<>(2);
        if (i == 1) {
            map.put("status",200);
            map.put("msg","下单成功！");
            Map<String, Object> map1 = stockClient.reductStock(order);
            log.info("库存服务反馈状态：{}",map1);
//            异常
//            int a = 1/0;
            return map;
        }
        map.put("status",500);
        map.put("msg","下单失败！");
        return map;
    }
}
